﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Crm.Sdk;
using System.Text.RegularExpressions;

/**
 * Copyright 2011 by Alberto Gemin
 * agemin@hotmail.com
 * Version 1.7.0.1
 * 13 Dec 2011
 **/
namespace Gemina.CRM2011.WF.LdgManager
{

    #region Enums

    public enum UserQueryQueryTypeEnum
    {
        AddressBookFilters = 512,
        AdvancedSearch = 1,
        CustomDefinedView = 16384,
        InteractiveWorkflowView = 4096,
        LookupView = 64,
        MainApplicationView = 0,
        MainApplicationViewWithoutSubject = 1024,
        OfflineFilters = 16,
        OutlookFilters = 256,
        QuickFindSearch = 4,
        Reporting = 8,
        SMAppointmentBookView = 128,
        SavedQueryTypeOther = 2048,
        SubGrid = 2
    };

    public enum SavedQueryQueryTypeEnum
    {
        AddressBookFilters = 512,
        AdvancedSearch = 1,
        CustomDefinedView = 16384,
        InteractiveWorkflowView = 4096,
        LookupView = 64,
        MainApplicationView = 0,
        MainApplicationViewWithoutSubject = 1024,
        OfflineFilters = 16,
        OfflineTemplate = 8192,
        OutlookFilters = 256,
        OutlookTemplate = 131072,
        QuickFindSearch = 4,
        Reporting = 8,
        SMAppointmentBookView = 128,
        SavedQueryTypeOther = 2048,
        SubGrid = 2
    }

    public enum ActionEnum
    {
        Error,
        Ignore,
        Update,
        Delete,
        Create
    }

    #endregion

    static public class AgViewsUtils
    {

        #region Constants

        public const string GroupName = "LDG AG Utilities";
        public const string ConstTrue = "true";

        public const string MsgQueryTypeSystem = "{0} is not a valid {1} for {2}. Valid values are 0,1,2,4,8,16,64,128,256,512,1024,2048,4096,8192,16384,131072 or any of AddressBookFilters,AdvancedSearch,CustomDefinedView,InteractiveWorkflowView,LookupView,MainApplicationView,MainApplicationViewWithoutSubject,OfflineFilters,OfflineTemplate,OutlookFilters,OutlookTemplate,QuickFindSearch,Reporting,SMAppointmentBookView,SavedQueryTypeOther,SubGrid.";
        public const string MsgQueryTypeUser = "{0} is not a valid {1} for {2}. Valid values are 0,1,2,4,8,16,64,128,256,512,1024,2048,4096,16384 or any of AddressBookFilters,AdvancedSearch,CustomDefinedView,InteractiveWorkflowView,LookupView,MainApplicationView,MainApplicationViewWithoutSubject,OfflineFilters,OutlookFilters,QuickFindSearch,Reporting,SMAppointmentBookView,SavedQueryTypeOther,SubGrid.";

        public const string FieldDescription = "description";
        public const string FieldQueryType = "querytype";
        public const string FieldIsDefault = "isdefault";
        public const string FieldFetchXml = "fetchxml";
        public const string FieldLayoutXml = "layoutxml";
        public const string FieldName = "name";
        public const string FieldOwner = "owner";
        public const string FieldReturnedTypeCode = "returnedtypecode";
        public const string FieldSavedQueryId = "savedqueryid";
        public const string FieldParentQueryId = "parentqueryid";

        public const string EntityNameSavedQuery = "savedquery";
        public const string EntityNameUserQuery = "userquery";
        public const string EntityNameSystemUser = "systemuser";

        #endregion

        #region Helper Methods

        internal static int? GetQueryType(string queryType, bool isSaved, string strParamQueryType, string strParamQueryName)
        {
            if (string.IsNullOrEmpty(queryType))
            {
                return null;
            }

            int? intQueryType = null;
            try
            {
                intQueryType = Int32.Parse(queryType);
            }
            catch { }
            if (isSaved)
            {
                if (queryType != null)
                {
                    try
                    {
                        intQueryType = (int)Enum.Parse(typeof(SavedQueryQueryTypeEnum), queryType, true);
                    }
                    catch { }
                }
                if (intQueryType == null || !Enum.IsDefined(typeof(SavedQueryQueryTypeEnum), intQueryType))
                {
                    throw new AgErrorMessage(string.Format(AgViewsUtils.MsgQueryTypeSystem, queryType, strParamQueryType, strParamQueryName));
                }
            }
            else
            {
                if (queryType != null)
                {
                    try
                    {
                        intQueryType = (int)Enum.Parse(typeof(UserQueryQueryTypeEnum), queryType, true);
                    }
                    catch { }
                }
                if (intQueryType == null || !Enum.IsDefined(typeof(UserQueryQueryTypeEnum), intQueryType))
                {
                    throw new AgErrorMessage(string.Format(AgViewsUtils.MsgQueryTypeUser, queryType, strParamQueryType, strParamQueryName));
                }
            }

            return intQueryType;
        }

        internal static List<int> GetUserQueryTypes(Regex queryTypeRegex)
        {
            List<int> types = new List<int>();
            foreach (string enumString in Enum.GetNames(typeof(UserQueryQueryTypeEnum)))
            {
                int value = (int)Enum.Parse(typeof(UserQueryQueryTypeEnum), enumString);
                string enumValue = value.ToString();
                if (queryTypeRegex.IsMatch(enumString) || queryTypeRegex.IsMatch(enumValue))
                {
                    types.Add(value);
                }
            }
            return types;
        }

        #endregion

    }
}
